package cc.admin.modules.graduate.controller;

import cc.admin.modules.cas.entity.Result;
import cc.admin.modules.graduate.entity.DeleteUrl;
import cc.admin.modules.graduate.entity.UploadFileParams;
import cc.admin.modules.graduate.entity.uploadFileVo;
import cc.admin.modules.graduate.service.IMediaFileService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@RestController
@Slf4j
@RequestMapping
public class UploadFileController {

    @Autowired
    private IMediaFileService mediaFileService;

    @PostMapping(value="/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public uploadFileVo uploadFile(@RequestParam("uploadFile") MultipartFile filedata) throws IOException {

        log.info("文件上传：{}",filedata);
        UploadFileParams uploadFileParams = new UploadFileParams();
        //文件大小
        uploadFileParams.setFileSize(filedata.getSize());
        //文件名称
        uploadFileParams.setFilename(filedata.getOriginalFilename());//文件名称
        //文件大小
//        long fileSize = filedata.getSize();
//        uploadFileParams.setFileSize(fileSize);
        //创建临时文件
        File tempFile = File.createTempFile("minio", "temp");
        //上传的文件拷贝到临时文件
        filedata.transferTo(tempFile);
        //文件路径
        String absolutePath = tempFile.getAbsolutePath();
        //上传文件
        uploadFileVo uploadFileVo = mediaFileService.uploadFile("subjectId", uploadFileParams, absolutePath);
        return uploadFileVo;
    }

    @PostMapping("/deleteVideo")
    public Result deleteVideo(@RequestBody DeleteUrl deleteUrl){
        String url = deleteUrl.getUrl();
        log.info("删除文件，文件路径{}",url);
        boolean b = mediaFileService.deleteVideo(url);
        if (b) {
            return new Result().ok("文件删除成功");
        } else {
            return new Result().error("文件删除失败");
        }
    }
}
